package com.phpxiu.app.pulltorefresh;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Matrix;
import android.graphics.Typeface;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.pulltorefresh.PullToRefreshView.Mode;
import com.phpxiu.app.pulltorefresh.PullToRefreshView.Orientation;
/**
 * @file
 * @brief 下拉刷新上拉加载控件，头部自定义视图
 * @author 吴金洲
 * @note  下拉刷新上拉加载控件，头部自定义视图
 *
 */
public class HeaderLoadingLayout extends LoadingLayout {
    static final Interpolator ANIMATION_INTERPOLATOR = new LinearInterpolator();
    static final int ROTATION_ANIMATION_DURATION = 1200;
    private final Animation mRotateAnimation;
    private final Matrix mHeaderImageMatrix;
    private float mRotationPivotX, mRotationPivotY;
    private FrameLayout mInnerLayout;
    protected final ImageView mHeaderLoading;
    protected final Mode mMode;
    protected final Orientation mScrollDirection;
    private AnimationDrawable loadingAnim=null;
	public HeaderLoadingLayout(Context context, Mode mode, Orientation scrollDirection, TypedArray attrs) {
		super(context, mode, scrollDirection, attrs);
        mMode = mode;
        mScrollDirection = scrollDirection;
        LayoutInflater.from(context).inflate(R.layout.pull_to_refresh_header, this);
        mInnerLayout = (FrameLayout) findViewById(R.id.fl_inner);
        mHeaderLoading= (ImageView) mInnerLayout.findViewById(R.id.pull_to_refresh_image);
        LayoutParams lp = (LayoutParams) mInnerLayout.getLayoutParams();
        switch (mode) {
            case PULL_FROM_END:
                lp.gravity = scrollDirection == Orientation.VERTICAL ? Gravity.TOP : Gravity.LEFT;
                break;
            case PULL_FROM_START:
            default:
                lp.gravity = scrollDirection == Orientation.VERTICAL ? Gravity.BOTTOM : Gravity.RIGHT;
                break;
        }
        if (attrs.hasValue(R.styleable.PullToRefresh_ptrHeaderBackground)) {
            Drawable background = attrs.getDrawable(R.styleable.PullToRefresh_ptrHeaderBackground);
            if (null != background) {
                PullToRefreshHelper.setBackground(this, background);
            }
        }
        // Try and get defined drawable from Attrs
        if (attrs.hasValue(R.styleable.PullToRefresh_ptrDrawable)) {
            loadingAnim = (AnimationDrawable)attrs.getDrawable(R.styleable.PullToRefresh_ptrDrawable);
        }
        // If we don't have a user defined drawable, load the default
        if (null == loadingAnim) {
            loadingAnim =(AnimationDrawable) context.getResources().getDrawable(getDefaultDrawableResId());
        }
        // Set Drawable, and save width/height
        setLoadingDrawable(loadingAnim);//设置动画背景
        reset();
        mHeaderLoading.setScaleType(ImageView.ScaleType.MATRIX);
        mHeaderImageMatrix = new Matrix();
        mHeaderLoading.setImageMatrix(mHeaderImageMatrix);
        mRotateAnimation = new RotateAnimation(0,720,Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        mRotateAnimation.setInterpolator(ANIMATION_INTERPOLATOR);
        mRotateAnimation.setDuration(ROTATION_ANIMATION_DURATION);
        mRotateAnimation.setRepeatCount(Animation.INFINITE);
        mRotateAnimation.setRepeatMode(Animation.RESTART);
	}

    @Override
    public void showInvisibleViews() {
        if (View.INVISIBLE == mHeaderLoading.getVisibility()) {
            mHeaderLoading.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideAllViews() {
        if (View.VISIBLE == mHeaderLoading.getVisibility()) {
            mHeaderLoading.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 下拉过程：
     * @param scale
     */
    public final void onPull(float scale) {
        float angle = scale * 90f;
        mHeaderImageMatrix.setRotate(angle, mRotationPivotX, mRotationPivotY);
        mHeaderLoading.setImageMatrix(mHeaderImageMatrix);
    }

    @Override
    public  void refreshing() {
        if(mHeaderLoading.getDrawable()!=null){
            if(loadingAnim!=null){
                loadingAnim.start();
            }
        }
    }

    @Override
    public void reset() {
        mHeaderLoading.setVisibility(View.VISIBLE);
        if(loadingAnim!=null){
            loadingAnim.stop();
        }
    }

    /**
     * 设置动画背景
     * @param loadingDrawable
     */
    public final void setLoadingDrawable(Drawable loadingDrawable) {
        mHeaderLoading.setImageDrawable(loadingDrawable);
        onLoadingDrawableSet(loadingDrawable);
    }

    public void onLoadingDrawableSet(Drawable imageDrawable) {
        if (null != imageDrawable) {
            mRotationPivotX = Math.round(imageDrawable.getIntrinsicWidth() / 2f);
            mRotationPivotY = Math.round(imageDrawable.getIntrinsicHeight() / 2f);
        }
    }

    public final void setHeight(int height) {
        ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) getLayoutParams();
        lp.height = height;
        requestLayout();
    }

    public final int getContentSize() {
        return mInnerLayout.getHeight();
    }

	@Override
	protected int getDefaultDrawableResId() {
		return R.drawable.gray_loading;
	}

    protected void onPullImpl(float scale) {
        // TODO NOTHING
    }

    @Override
    protected void resetImpl() {
        // TODO NOTHING
    }
    @Override
    protected void pullToRefreshImpl() {
        // TODO NOTHING
    }

    public void pullToRefresh(){
        //TODO NOTHING
    }

    public void releaseToRefresh() {
        //TODO NOTHING
    }

    @Override
    protected void refreshingImpl() {
        // TODO NOTHING
    }

    @Override
    protected void releaseToRefreshImpl() {
        // TODO NOTHING
    }

    @Override
    public void setTextTypeface(Typeface tf) {
        // TODO NOTHING
    }

    @Override
    public void setLastUpdatedLabel(CharSequence label) {
        // TODO NOTHING
    }

    public void setPullLabel(CharSequence pullLabel) {
        // TODO NOTHING
    }

    public void setRefreshingLabel(CharSequence refreshingLabel) {
        // TODO NOTHING
    }

    public void setReleaseLabel(CharSequence releaseLabel) {
        // TODO NOTHING
    }

}
